package com.beatingapp;

import java.util.Timer;
import java.util.TimerTask;

import org.andengine.engine.camera.Camera;
import org.andengine.engine.options.EngineOptions;
import org.andengine.engine.options.ScreenOrientation;
import org.andengine.engine.options.resolutionpolicy.FillResolutionPolicy;
import org.andengine.entity.scene.Scene;
import org.andengine.entity.sprite.Sprite;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.andengine.opengl.texture.region.ITextureRegion;
import org.andengine.opengl.texture.region.TextureRegion;
import org.andengine.opengl.vbo.VertexBufferObjectManager;
import org.andengine.ui.activity.SimpleBaseGameActivity;

public class FelicidadesActivity extends SimpleBaseGameActivity 
{

	private final static int CAMERA_WIDTH = 800;

	private final static int CAMERA_HEIGHT = 480;
	
	private Camera camera;

	private BitmapTextureAtlas fondoTexture;

	private TextureRegion fondoTextureRegion;
	
	private boolean inicia;

	private int vidas;

	private BitmapTextureAtlas vidasTextureAtlas, sinVidasTextureAtlas;

	private ITextureRegion vidaTextureRegion, sinVidaTextureRegion;
	
	@Override
	public EngineOptions onCreateEngineOptions() 
	{		
		camera = new Camera( 0, 0, CAMERA_WIDTH, CAMERA_HEIGHT );
		EngineOptions engineOptions = new EngineOptions( true, ScreenOrientation.LANDSCAPE_SENSOR,
				new FillResolutionPolicy(), camera );
		engineOptions.getAudioOptions( ).setNeedsSound( true );
		return engineOptions;
	}

	@Override
	protected void onCreateResources() 
	{
		BitmapTextureAtlasTextureRegionFactory.setAssetBasePath( "gfx/" );
		this.fondoTexture = new BitmapTextureAtlas( this.getTextureManager( ), 1024, 512 );
		this.fondoTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(
				this.fondoTexture, this, "felicidades.png", 0, 0 );
		this.fondoTexture.load( );
		inicia = true;
		
		BitmapTextureAtlasTextureRegionFactory.setAssetBasePath( "gfx/vidas/" );

		//vidas
		this.vidasTextureAtlas = new BitmapTextureAtlas( this.getTextureManager( ), 80, 80 );
		this.vidaTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(
				this.vidasTextureAtlas, this, "vidaG.png", 0, 0 );
		this.vidasTextureAtlas.load( );

		this.sinVidasTextureAtlas = new BitmapTextureAtlas( this.getTextureManager( ), 80, 80 );
		this.sinVidaTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(
				this.sinVidasTextureAtlas, this, "sinVidaG.png", 0, 0 );
		this.sinVidasTextureAtlas.load( );

		vidas = getIntent( ).getExtras( ).getInt( "Vidas" );
	}

	@Override
	protected Scene onCreateScene() 
	{
		Scene scene = new Scene( );
		Sprite fondoSprite = new Sprite( 0, 0, fondoTextureRegion, new VertexBufferObjectManager( ) );
		scene.attachChild( fondoSprite );
		
		Timer t = new Timer();  

		TimerTask task = new TimerTask() {  

			@Override  
			public void run() {  
				runOnUiThread(new Runnable() {  

					@Override  
					public void run() 
					{  		      
						if( inicia )
						{
							try 
							{
								Thread.sleep(3000);
							} 
							catch (InterruptedException e) 
							{
								e.printStackTrace();
							}
							inicia = false;
							FelicidadesActivity.this.finish();
						}
					}  
				});  
			}  
		};
		t.scheduleAtFixedRate(task, 0, 1000);
		
		for (int i = 1; i <= 3; i++) 
		{
			if( vidas >= i )
			{
				Sprite vidas = new Sprite( (80*i) + 190, 180, vidaTextureRegion, new VertexBufferObjectManager( ) );
				scene.attachChild( vidas );
			}
			else
			{
				Sprite vidas = new Sprite( (80*i) + 190, 180, sinVidaTextureRegion, new VertexBufferObjectManager( ) );
				scene.attachChild( vidas );
			}
		}
		
		return scene;
	}
}
